package com.finchina.myview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

/**
 * 作者：黄赛赛 on 2015/9/21 10:16
 *
 * @version V1.0
 * @Description: ${todo}()
 */
public class DreamEffectView extends View {
    private Paint mBitmapPaint;// 位图画笔
    private Paint  mShaderPaint;// 位图画笔和Shader图形的画笔
    private Bitmap mBitmap;// 位图
    private Bitmap  darkCornerBitmap;// 源图的Bitmap和我们自己画的暗角Bitmap
    private PorterDuffXfermode mXfermode;// 图形混合模式
    private int x, y;// 位图起点坐标
    private int screenW, screenH;// 屏幕宽高
    public DreamEffectView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.mm2);

        mBitmapPaint  = new Paint(Paint.ANTI_ALIAS_FLAG);

        // 去饱和、提亮、色相矫正
        mBitmapPaint.setColorFilter(new ColorMatrixColorFilter(new float[] { 0.8587F, 0.2940F, -0.0927F, 0, 6.79F, 0.0821F, 0.9145F, 0.0634F, 0, 6.79F, 0.2019F, 0.1097F, 0.7483F, 0, 6.79F, 0, 0, 0, 1, 0 }));

        // 实例化混合模式
        mXfermode = new PorterDuffXfermode(PorterDuff.Mode.SCREEN);

        mShaderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);


        //源图的Bitmap和我们自己画的暗角Bitmap
        darkCornerBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_8888);

        // 将该暗角Bitmap注入Canvas
        Canvas canvas = new Canvas(darkCornerBitmap);

        // 计算径向渐变半径
        float radiu = canvas.getHeight() * (2F / 3F);

        // 实例化径向渐变
        float cx = canvas.getWidth() / 2F;
        float cy = canvas.getHeight() / 2F;
        RadialGradient radialGradient = new RadialGradient(cx, cy, radiu, new int[] { 0, 0, 0xAA000000 }, new float[] { 0F, 0.7F, 1.0F }, Shader.TileMode.CLAMP);

        // 实例化一个矩阵
        Matrix matrix = new Matrix();

        // 设置矩阵的缩放
        float scaleX = canvas.getWidth() / (radiu * 2F);
        matrix.setScale(scaleX, 1.0F);

        // 设置矩阵的预平移
//      float translateX = ((radiu * 2F) - canvas.getWidth()) / 2F;
        float translateX =radiu * (1 -scaleX);
        matrix.preTranslate(translateX, 0);

        // 将该矩阵注入径向渐变
        radialGradient.setLocalMatrix(matrix);

        // 设置画笔Shader
        mShaderPaint.setShader(radialGradient);

        // 绘制矩形
        canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mShaderPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        screenW = getMeasuredWidth();
        screenH = getMeasuredHeight();
        x = (screenW - mBitmap.getWidth() )/2;
        y = (screenH - mBitmap.getHeight())/2;

        mShaderPaint.setShader(new RadialGradient(screenW/2, screenH/2,  mBitmap.getHeight()*7.0f /8.0f,Color.TRANSPARENT, Color.BLACK, Shader.TileMode.CLAMP));
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        canvas.drawColor(Color.BLACK);
        // 新建图层
        int sc = canvas.saveLayer(x,y, x+mBitmap.getWidth(), y+mBitmap.getHeight(), null, Canvas.ALL_SAVE_FLAG);
        // 绘制混合颜色
        canvas.drawColor(0xcc1c093e);
        // 设置混合模式
        mBitmapPaint.setXfermode(mXfermode);
        // 绘制位图s
        canvas.drawBitmap(mBitmap, x, y, mBitmapPaint);
        // 还原混合模式
        mBitmapPaint.setXfermode(null);
        // 还原画布
        canvas.restoreToCount(sc);

        // 绘制一个跟图片大小一样的矩形
//        canvas.drawRect(x, y, x + mBitmap.getWidth(), y + mBitmap.getHeight(), mShaderPaint);

        // 绘制我们画好的径向渐变图
        canvas.drawBitmap(darkCornerBitmap, x, y, null);
    }
}
